<?php
declare(strict_types=1);

namespace App\ZxAdmin\Users;

use App\Controller;
use Field\SysDictConst;
use Field\SysRoleConst;

// hook controller_zx_admin_users_role_use.php

/**
 * @title   权限组
 * @auth    true
 * @login   true
 * @menu    true
 * @component   vlist/index.vue
 * @icon    el-icon-menu
 * @active  true
 * @affix   false
 * @rank    1
 */
class Role extends Controller
{
    // hook controller_zx_admin_users_role_public_start.php

    /**
     * @title   列表配置
     */
    public const Conf = [
        'Index' => 'RoleID',
        'TreeData' => true,
        'rowField' => 'RoleID',
        'parentField' => 'ParentRoleID',
        'pageSize' => 25,
        'remoteSort' => 0,
        'remoteFilter' => 0,
        'remoteSummary' => 0,
        'hidePagination' => 1,
        'hideRefresh' => 0,
        'hideSetting' => 0,
        'hideDo' => 0,
        'add' => 1,
        'start' => 1,
        'stop' => 1,
        'delete' => 1,
        'import' => 0,
        'export' => 0
    ];

    // hook controller_zx_admin_users_role_public_end.php

    /**
     * @title  下发配置
     * @rank    0
     */
    public function action_Conf_PUT()
    {
        // hook controller_zx_admin_users_role_conf_start.php
        $Columns = [
            [
                'label' => "操作",
                'type' => "button",
                'options' => [
                    ['Name' => 'Add', "Do" => "Add", 'Icon' => 'el-icon-plus']
                ],
                'width' => "50"
            ],
            [
                'label' => "权限组ID",
                'prop' => "RoleID",
                'width' => "100"
            ],[
                'label' => "权限组名称",
                'prop' => "RoleName",
                'editable'=>1,
                'width' => "100"
            ],[
                'type'=>'textarea',
                'label' => "权限组简介",
                'prop' => "Intro",
                'editable'=>1,
                'width' => "500",
                'cellEditorParams'=>[
                    'maxLength'=> 100
                ],
            ],
            [
                'type' => "table",
                'label' => "权限",
                'prop' => "AuthCount",
                'editable'=>1,
                'width' => "60",
                'apiObjRemote'=>'/zxadmin/users/roleauth'
            ],
            [
                'type' => "dict",
                'label' => "状态",
                'editable'=>1,
                'prop' => "Status",
                'width' => "60"
            ],
        ];
        $tabs = [];
        $tabs[] = [
            'Name' => '管理员',
            'Index' => '1'
        ];
        $tabs[] = [
            'Name' => '员工',
            'Index' => '1000'
        ];
        $tabs[] = [
            'Name' => '用户',
            'Index' => '10000'
        ];

        // hook controller_zx_admin_users_role_conf_end.php
        $this->response('0000', ['Tabs' => $tabs,'Conf' => self::Conf, 'Filter' => [], 'Tools' => [], 'Columns' => $Columns]);
    }

    /**
     * @title   下发列表数据
     * @rank    73
     */
    public function action_Index_GET()
    {
        // hook controller_zx_admin_users_role_index_start.php
        $group = $this->request->param('tab', 1);
        $cond = [
            SysRoleConst::RoleType=>$group
        ];
        $data = $this->SysRole->GetList($cond);
        $auth =$this->SysRoleAuth->select([],[],0,0,'RoleID,SUM(Status) as Count','RoleID','Group By RoleID');
        foreach ($data['data'] as &$row){
            $row['AuthCount'] = !empty($auth[$row['RoleID']]) ? (int)$auth[$row['RoleID']]['Count'] : 0;
        }
        // hook controller_zx_admin_users_role_index_end.php
        $this->response('0000', $data);
    }

    /**
     * @title   新增配置
     * @rank    72
     */
    public function action_Add_PUT()
    {
        // hook controller_zx_admin_users_role_add_put_start.php

        $data = [
            [
                'label' => "权限组名称",
                'prop' => "RoleName",
                'span' => 24,
                'component' => 'input',
                'options' => ['maxlength' => 32, 'placeholder' => '请输入权限组名称'],
                'rules' => [
                    ['required' => true, 'message' => '请输入权限组名称']
                ],
                'requiredHandle' => "$.required==true"
            ],
            [
                'label' => "描述",
                'prop' => "Intro",
                'value' => '',
                'span' => 24,
                'component' => 'input',
                'options' => ['maxlength' => 120, 'placeholder' => '请输入描述'],
                'rules' => [
                    ['required' => true, 'message' => '请输入描述']
                ],
                'requiredHandle' => "$.required==true"
            ]
        ];

        // hook controller_zx_admin_users_role_add_put_end.php
        $this->response('0000', ['data' => $data, 'title' => '添加权限','width'=>'500px']);
    }

    /**
     * @title   新增提交
     * @rank    71
     */
    public function action_Add_POST()
    {
        // hook controller_zx_admin_users_role_add_post_start.php
        $data = [];
        $group = $this->request->param('tab', 1);
        $cond = [
            SysRoleConst::RoleType=>$group
        ];
        $data['RoleName'] = $this->request->param('RoleName');
        $data['Intro'] = $this->request->param('Intro');
        $this->CheckEmpty([$data['RoleName']], ['名字']);
        $max = $this->SysRole->max('RoleID',$cond);
        $max+=1;
        $data['RoleID'] = $max;
        $data['Status'] = 1;
        $data['RoleType'] = $group;
        $r = $this->SysRole->insert($data) ;
        // hook controller_zx_admin_users_role_add_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    /**
     * @title   编辑配置
     * @rank    63
     */
    public function action_Edit_PUT()
    {
        // hook controller_zx_admin_users_role_edit_put_start.php
        $data = [];
        // hook controller_zx_admin_users_role_edit_put_end.php
        $this->response('0000', ['data' => $data,'title' => '编辑权限','width'=>'800px']);
    }

    /**
     * @title   编辑数据
     * @rank    62
     */
    public function action_Edit_GET()
    {
        // hook controller_zx_admin_users_role_edit_put_start.php
        $cond = [];
        $cond[SysRoleConst::RoleID] = $this->request->param(SysRoleConst::RoleID, 0);
        $data = $this->SysRole->readByIndex($cond)->toArray();
        // hook controller_zx_admin_users_role_edit_put_end.php
        $this->response('0000', ['data' => $data]);
    }

    /**
     * @title   编辑保存
     * @rank    61
     */
    public function action_Edit_POST()
    {
        // hook controller_zx_admin_users_role_edit_start.php
        $cond = [];
        $cond[SysRoleConst::RoleID] = $this->request->param(SysRoleConst::RoleID, 0);
        $data = [];
        $data['RoleName'] = $this->request->param('RoleName');
        $data['Intro'] = $this->request->param('Intro');
        $data['Status'] = $this->request->param('Status', 0);
        $r = $this->SysRole->update($cond, $data);
        // hook controller_zx_admin_users_role_edit_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller_zx_admin_users_role_field_post_start.php
        $cond = [];
        $cond[SysRoleConst::RoleID] = $this->request->param(SysRoleConst::RoleID, 0);
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field], ['待修改字段']);
        $field_fields = ['RoleName', 'Intro', 'Status'];
        !in_array($field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        !in_array($field, SysRoleConst::fields, true) && $this->response('0001', [], '字段不允许修改');
        $update = [];
        switch ($field) {
            case 'RoleName':
            case 'Intro':
                $update[$field] = $value;
                break;
            case 'Status':
                $update['Status'] = intval($value);
                break;
        }
        $r = !empty($update) ? $this->SysRole->update($cond, $update) : false;
        // hook controller_zx_admin_users_role_field_post_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }


    /**
     * @title   删除数据
     * @rank    50
     */
    public function action_Index_Delete()
    {
        // hook controller_zx_admin_users_role_delete_start.php
        $data = $this->request->param('data', []);
        $this->CheckEmpty([$data], ['操作数据为空']);
        $r = $this->SysRole->deleteByIndex($data);
        // hook controller_zx_admin_users_role_delete_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }

    /**
     * @title   启用
     * @rank    41
     */
    public function action_Enable_POST()
    {
        // hook controller_zx_admin_users_role_enable_start.php
        $data = $this->request->param('data', []);
        $this->CheckEmpty([$data], ['操作数据为空']);
        $r = $this->SysRole->updateByIndex($data, [SysDictConst::Status => 1]);
        // hook controller_zx_admin_users_role_enable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }

    /**
     * @title   禁用
     * @rank    40
     */
    public function action_Disable_POST()
    {
        // hook controller_zx_admin_users_role_disable_start.php
        $data = $this->request->param('data', []);
        $this->CheckEmpty([$data], ['操作数据为空']);
        $r = $this->SysRole->updateByIndex($data, [SysDictConst::Status => 0]);
        // hook controller_zx_admin_users_role_disable_end.php
        $this->response($r ? '0000' : '0001', ['data' => $r]);
    }

    // hook controller_zx_admin_users_role_end.php
}

?>